package com.example.mahao_final.adapter

import android.app.AlertDialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.mahao_final.R
import com.example.mahao_final.entity.Blog

class MyBlogAdapter(
    private var blogs: List<Blog>,
    private val context: Context,  // 添加上下文参数以便显示对话框
    private val onBlogClick: (Blog) -> Unit,  // 点击博客项
    private val onEditClick: (Blog) -> Unit,  // 点击编辑按钮
    private val onDeleteClick: (Blog) -> Unit // 点击删除按钮
) : RecyclerView.Adapter<MyBlogAdapter.BlogViewHolder>() {

    // 更新博客列表数据的方法
    fun updateList(newBlogs: List<Blog>) {
        blogs = newBlogs
        notifyDataSetChanged()  // 通知 RecyclerView 更新数据
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BlogViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_myblog, parent, false)
        return BlogViewHolder(view)
    }

    // 实现 onBindViewHolder 方法
    override fun onBindViewHolder(holder: BlogViewHolder, position: Int) {
        val blog = blogs[position]
        holder.bind(blog)
    }

    override fun getItemCount(): Int {
        return blogs.size
    }

    // ViewHolder 内部类
    inner class BlogViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvTitle: TextView = itemView.findViewById(R.id.tv_title)
        private val tvDescription: TextView = itemView.findViewById(R.id.tv_description)
        private val tvAuthor: TextView = itemView.findViewById(R.id.tv_author)
        private val tvDifficulty: TextView = itemView.findViewById(R.id.tv_difficulty)
        private val tvTags: TextView = itemView.findViewById(R.id.tv_tags)

        private val btnEdit: Button = itemView.findViewById(R.id.btn_edit)
        private val btnDelete: Button = itemView.findViewById(R.id.btn_delete)

        fun bind(blog: Blog) {
            tvTitle.text = blog.title
            tvDescription.text = blog.description
            tvAuthor.text = "作者: ${blog.author}"
            tvDifficulty.text = "难度: ${blog.difficulty}"
            tvTags.text = "标签: ${blog.tags.joinToString(", ")}"

            // 设置博客项点击事件
            itemView.setOnClickListener {
                onBlogClick(blog)
            }

            // 设置编辑按钮点击事件
            btnEdit.setOnClickListener {
                onEditClick(blog)
            }

            // 设置删除按钮点击事件，弹出确认删除对话框
            btnDelete.setOnClickListener {
                showDeleteConfirmationDialog(blog)
            }
        }

        private fun showDeleteConfirmationDialog(blog: Blog) {
            val builder = AlertDialog.Builder(context)
            builder.setTitle("确认删除")
                .setMessage("你确定要删除这篇博客吗？")
                .setPositiveButton("确定") { dialog, _ ->
                    onDeleteClick(blog)  // 执行删除操作
                    dialog.dismiss()
                }
                .setNegativeButton("取消") { dialog, _ ->
                    dialog.dismiss()  // 关闭对话框
                }
                .show()
        }
    }
}
